/*
*  Copyright 2019-2020
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package com.qp.basics.admin.controller;

import com.qp.basics.admin.service.OauthClientDetailService;
import com.qp.basics.admin.service.dto.OauthClientDetailQueryCriteria;
import com.qp.basics.common.annotation.Log;
import com.qp.basics.common.domain.OauthClientDetail;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@RestController
@RequiredArgsConstructor
@Api(tags = "管理")
@RequestMapping("/api/oauthClientDetail")
public class OauthClientDetailController {

    private final OauthClientDetailService oauthClientDetailService;

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('oauthClientDetail:list')")
    public void download(HttpServletResponse response, OauthClientDetailQueryCriteria criteria) throws IOException {
        oauthClientDetailService.download(oauthClientDetailService.queryAll(criteria), response);
    }

    @GetMapping
    @Log("查询")
    @ApiOperation("查询")
    @PreAuthorize("@el.check('oauthClientDetail:list')")
    public ResponseEntity<Object> query(OauthClientDetailQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(oauthClientDetailService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增")
    @ApiOperation("新增")
    @PreAuthorize("@el.check('oauthClientDetail:add')")
    public ResponseEntity<Object> create(@Validated @RequestBody OauthClientDetail resources){
        return new ResponseEntity<>(oauthClientDetailService.create(resources),HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改")
    @ApiOperation("修改")
    @PreAuthorize("@el.check('oauthClientDetail:edit')")
    public ResponseEntity<Object> update(@Validated @RequestBody OauthClientDetail resources){
        oauthClientDetailService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @Log("删除")
    @ApiOperation("删除")
    @PreAuthorize("@el.check('oauthClientDetail:del')")
    @DeleteMapping
    public ResponseEntity<Object> delete(@RequestBody Long[] ids) {
        oauthClientDetailService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }
    @ApiOperation("导入")
    @PostMapping(value = "/import")
    public ResponseEntity<Object> importExcel(@RequestParam("file") MultipartFile file){
        return new ResponseEntity<Object>(oauthClientDetailService.importExcel(file),HttpStatus.OK);
    }
}
